from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class PageNum(PageNumberPagination):
    # 请求每页的页容量，以及该参数的命名
    page_size = 3
    page_size_query_param = 'pagesize'

    # 请求页码的参数命名
    page_query_param = 'page'

    # 后端向前段规定每页的最大请求量
    max_page_size = 10

    def get_paginated_response(self, data):
        """
        {
            "counts": "用户总量",
            "lists": [
                {
                    "id": "用户id",
                    "username": "用户名",
                    "mobile": "手机号",
                    "email": "邮箱"
                },
                ...
            ],
            "page": "页码",
            "pages": "总页数",
            "pagesize": "页容量"
          }
        """
        return Response({
            'count': self.page.paginator.count,
            'lists': data,
            'page': self.request.query_params.get('page'),
            'pages': self.page.paginator.num_pages,
            'pagesize': self.page_size
        })


